{literal}
    <style type="text/css">
		@import url('../static/admin/css/shippingtime.css');
		
		.action .btn24{
			margin-left: 4px;
			margin-right: 4px;
			float:left;
		}
		.for_editing
		{
			display: none;
		}
    </style>
{/literal}

{include file = "styles/header.tpl"}
<div id="content" class="box">
    <h1>
	Shipping Time Manager - {if $data_type == 'rule_active'} Active {else} Pending {/if}
	Rules </h1>
    <form method="post" id="frmRules" name="frmRules" action="{$smarty.const.HOST_ADMIN}/shippingtime/importrule" enctype="multipart/form-data">
        <fieldset style="float:left;width:25%;height:50px">
			<legend>Import</legend>			
			<div >
				<input type="file" name="rules_file" id="rules_file" style="padding:0px;font-size: 10pt;">
				<button type="submit" style="padding:3px;font-size: 10pt;" value="import">Import</button>	
			</div>			
        </fieldset>
		<fieldset style="float:right;width:70%;height:50px">
			<legend>Test:</legend>
			<div >
				Location:
				<select name="location" id="location">
					<option value="HCM">Hồ Chí Minh</option>
					<option value="AG">An Giang</option>
					<option value="BRVT">Bà Rịa - Vũng Tàu </option>
					<option value="BL">Bạc Liêu</option>
					<option value="BG">Bắc Giang</option>
					<option value="BK">Bắc Kạn</option>
					<option value="BN">Bắc Ninh</option>
					<option value="BT">Bến Tre</option>
					<option value="BD">Bình Dương</option>
					<option value="BDi">Bình Định</option>
					<option value="BP">Bình Phước</option>
					<option value="BTh">Bình Thuận</option>
					<option value="CM">Cà Mau</option>
					<option value="CB">Cao Bằng</option>
					<option value="CT">Cần Thơ</option>
					<option value="DNa">Đà Nẵng</option>
					<option value="DL">Đắk Lắk</option>
					<option value="DNo">Đắk Nông</option>
					<option value="DB">Điện Biên</option>
					<option value="DN">Đồng Nai</option>
					<option value="DT">Đồng Tháp</option>
					<option value="GL">Gia Lai</option>
					<option value="HG">Hà Giang</option>
					<option value="HNa">Hà Nam</option>
					<option value="HN">Hà Nội</option>
					<option value="HT">Hà Tĩnh</option>
					<option value="HD">Hải Dương</option>
					<option value="HP">Hải Phòng</option>
					<option value="HG">Hậu Giang</option>
					<option value="HB">Hòa Bình</option>
					<option value="Hue">Huế</option>
					<option value="HY">Hưng Yên</option>
					<option value="KG">Kiên Giang</option>
					<option value="KT">Kon Tum</option>
					<option value="KH">Khánh Hòa</option>
					<option value="LC">Lai Châu</option>
					<option value="LS">Lạng Sơn</option>
					<option value="LCa">Lào Cai</option>
					<option value="LD">Lâm Đồng</option>
					<option value="LA">Long An</option>
					<option value="ND">Nam Định</option>
					<option value="NB">Ninh Bình</option>
					<option value="NT">Ninh Thuận</option>
					<option value="NA">Nghệ An</option>
					<option value="PT">Phú Thọ</option>
					<option value="PY">Phú Yên</option>
					<option value="QB">Quảng Bình</option>
					<option value="QNa">Quảng Nam</option>
					<option value="QN">Quảng Ninh</option>
					<option value="QNg">Quảng Ngãi</option>
					<option value="QT">Quảng Trị</option>
					<option value="ST">Sóc Trăng</option>
					<option value="SL">Sơn La</option>
					<option value="TN">Tây Ninh</option>
					<option value="TG">Tiền Giang</option>
					<option value="TQ">Tuyên Quang</option>
					<option value="TB">Thái Bình</option>
					<option value="TNg">Thái Nguyên</option>
					<option value="TH">Thanh Hóa</option>
					<option value="TTH">Thừa Thiên Huế</option>
					<option value="TV">Trà Vinh</option>
					<option value="VL">Vĩnh Long</option>
					<option value="VP">Vĩnh Phúc</option>
					<option value="VT">Vũng Tàu</option>
					<option value="YB">Yên Bái</option>									
				</select>
				Sku:<input type="text" name="sku" value="LE659EL97FVYANVN-40285" id="sku" style="width:200px">
				Date:<input type="text" name="date" id="date">
				<button class="btn" id="test_shipping_service" style="font-size: 10pt;">Get Delivery Estimate</button>
				<span style="color:blue;font-size:14pt" id="result"></span>				
				<span style="color:red;font-size:12pt" id="error"></span>
				<input class="debug" name="debug" type="checkbox" style="display:none"></input>			
			</div>
			
		</fieldset>
		<div style="clear:both"></div>
		<fieldset>					
			<div >	
				{if $data_type == 'rule_active'}
					<a title="Approve all rule" class="btn" href="{$smarty.const.HOST_ADMIN}/shippingtime/listrule?data_type=rule_pending">View Pending Rules</a>					
				{else}
					<a title="Create a rule" class="btn" href="{$smarty.const.HOST_ADMIN}/shippingtime/createrule/order/null">Create Rule</a>				
					<a title="Export all rule" class="btn" href="{$smarty.const.HOST_ADMIN}/shippingtime/exportrule/">Export</a>				
					<a title="Approve all rule" class="btn" href="{$smarty.const.HOST_ADMIN}/shippingtime/approverule/">Approve</a>
					<a title="Approve all rule" class="btn" href="{$smarty.const.HOST_ADMIN}/shippingtime/listrule?data_type=rule_active">View Active Rules</a>
				{/if}					
			</div>			
        </fieldset>
	</form>
    <table id="rules_list" width="100%">
        <thead>
			<tr>            
				<th>Id</th>
				<th>Order</th>
				<th>Conditions</th>	
				<th>Result</th>				
				<th>Comment</th>
				<th>Enabled</th>
				<th class="last" style="text-align:center">Action</th>
			</tr>
		</thead>
		<tbody>
        {foreach  from=$rules key=k item=rule}			
            <tr class="rule_data">                
				<td class="rule_id" style="width:20px">
                    <span class="hidden">
                        <input type="text" class="rule_id" name="rule_id" value="{$rule.id}" >
                    </span>
                    <span>{$rule.id}</span>
                </td>
                <td class="rule_order" style="width:10px" align=center>
                    <span class="for_editing" style="width:100%">
                        <input type="text" class="" name="rule_order" value="{$rule.order}" style="width:40px">
                    </span>
                    <a title="move up" class="btn24 up"><span></span></a>
					<a title="move down" class="btn24 down"><span></span></a>
				</td>	                
                <td class="rule_cond" style="width:400px">
                    <span class="for_view">
                        {$rule.conditions_parsed}
                    </span>
                </td>
                <td class="rule_action" style="width:300px">
                    <span class="for_view">
                        {$rule.action_parsed}
                    </span>
                </td>
				<td class="rule_comment">
                    <span class="for_view">{$rule.comment}</span>
                </td>
                <td class="rule_enabled" style="width:10px" align="center">
                    {if $rule.enabled eq 1}
                        {assign var=checked value="checked"}
                    {else}
                        {assign var=checked value=""}
                    {/if}
					<input disabled class="rule_enabled_checkbox" name="rule_enabled" type="checkbox" {$checked} />
                </td>
                <td class="action" style="width:150px" align=center>
					{if $data_type == 'rule_active'}
						{assign var='cls' value='disabled'}
					{else}
						{assign var='cls' value=''}
					{/if}
                    <a title="edit" class="btn24 edit {$cls}" href="{$smarty.const.HOST_ADMIN}/shippingtime/editrule/rule_id/{$rule.id}"><span></span></a>
					<a title="clone" class="btn24 clone {$cls}" href="{$smarty.const.HOST_ADMIN}/shippingtime/editrule/rule_id/{$rule.id}?rule_clone=1"><span></span></a>
                    <a title="insert before" class="btn24 add {$cls}" href="{$smarty.const.HOST_ADMIN}/shippingtime/newrule/order/{$rule.order}"><span></span></a>
                    <!--<button class="btn reset"><span class=""></span></button>-->
                    <!--<button class="btn save disabled"><span class=""></span></button>-->
                    <!--<button title="insert before" class="btn add"><span class=""></span></button>                    -->
                    <a title="delete" class="btn24 delete {$cls}"><span class=""></span></button>                    
                </td>
            </tr>
        {/foreach}
	</tbody>
    </table>
</div>

<script type="text/javascript">
    var base_url = "{$smarty.const.HOST_ADMIN}";
	var data_type = "{$data_type}";
    {literal}
    $(function(){
		//init
		$(".for_editing").hide();		
		$(".btn.reset").hide();
		$("input#date").datepicker();
		$("input#date").datepicker("option","dateFormat","yy-mm-dd");
		
		/*
		$("#rules_list tbody").sortable({
			revert: true
		});
		$("tr.rule_data").draggable({
		  connectToSortable: "#rules_list tbody",
		  helper: "clone",
		  revert: "invalid"
		});
		*/	
		$("#content").delegate(".btn24.disabled", "click", function(){								
			return false;
		});
		
		$("#content").delegate(".btn24.clone", "click", function(){			
			if(confirm('Do you want to clone this rule?') == false)
				return false;
			return true;
		});
		
		$("#content").delegate(".btn24.delete", "click", function(){
			if($(this).hasClass('disabled') == true)
				return false;
				
			var tr_tag = $(this).parent().parent();			
			if(confirm("Do you want to delete this rule?") == false)
				return false;			
			deleteRule(tr_tag);						
			return false;
		});
		
		$("#content").delegate("a.btn24.up", "click", function(){
			var tr_tag = $(this).parent().parent();
			var prev_tr_tag =  tr_tag.prev();
			if(prev_tr_tag.hasClass("rule_data") == false)
				return false;
			swapRuleOrder(tr_tag, prev_tr_tag);					
			return false;
		});
		
		$("#content").delegate("a.btn24.down", "click", function(){
			var tr_tag = $(this).parent().parent();
			var next_tr_tag =  tr_tag.next();
			if(next_tr_tag.hasClass("rule_data") == false)
				return false;
			swapRuleOrder(tr_tag, next_tr_tag);					
			return false;
		});
		
		$("#test_shipping_service").click(function(){
			var data = new Object;
			data.location = $("select#location").val();
			data.sku = $("input#sku").val();
			if($("input#date").val())
				data.date = $("input#date").val();
			$("span#result").hide('fast');
			$("span#error").hide('fast');
			
			var debug = $("input.debug").attr('checked') == 'checked'? true:false;
			if(debug == true)
				data.debug = 1;
			
			if(data_type == 'rule_active')	
				data.rule_test = 0;			
			else
				data.rule_test = 1;
				
			var url_service = "http://ops.lazada.com/shippingtime_service/get_time_shipping";
			$.get(url_service, data, function(response){
				$("span#result").text("");
				$("span#error").text("");
				
				$("span#result").show('fast');
				$("span#error").show('fast');
			
				if(debug == true)
						console.log(response.debug);
						
				if(response.error == 1)
				{
					$("span#error").text(response.message);
					return;
				}
				
				if(response.result_type == 1)
				{
					$("span#result").text(" = "+response.data.result);					
				}
				else{
					$("span#result").text(" + "+response.data.result + '(h)');
				}								
			},'json');	
			return false;
		});
    });
	
	function enableRuleEdit(tr_tag)
	{
            tr_tag.find(".for_editing").show();
			tr_tag.find(".btn.reset").show();
            tr_tag.find(".for_view").hide();
			tr_tag.find(".btn.edit").hide();
			tr_tag.find(".btn.save").removeClass('disabled');			
			tr_tag.find("input.rule_enabled_checkbox").removeAttr('disabled');
	}
	
	function disableRuleEdit(tr_tag)
	{
            tr_tag.find(".for_editing").hide();
			tr_tag.find(".btn.reset").hide();
            tr_tag.find(".for_view").show();
			tr_tag.find(".btn.edit").show();
			tr_tag.find(".btn.save").addClass('disabled');			
			tr_tag.find("input.rule_enabled_checkbox").attr('disabled', 'disabled');
	}

	function saveRule(tr_tag)
	{		
            var data = tr_tag.find('input').serialize() + "&" + tr_tag.find('textarea').serialize();
            var url = base_url + '/shippingtime/updaterule';
            $.get(url, data, function(response){
				callbackSaveRule(tr_tag, response);				
			}, 'json');			
	}
	
	function deleteRule(tr_tag)
	{		
            var data = tr_tag.find('input.rule_id').serialize();
            var url = base_url + '/shippingtime/deleterule';
            $.get(url, data, function(response){
				callbackDeleteRule(tr_tag, response);				
			});			
	}
	
	function swapRuleOrder(tr_tag1, tr_tag2)
	{
		// swap number
		//var number1 = tr_tag1.find('span.number').text();
		//var number2 = tr_tag2.find('span.number').text();		
		//tr_tag1.find('span.number').text(number2);
		//tr_tag2.find('span.number').text(number1);
			
		var rule_id1 = tr_tag1.find('.rule_id input').val();
		var rule_id2 = tr_tag2.find('.rule_id input').val();
		var order1 = tr_tag1.find('.rule_order input').val();
		var order2 = tr_tag2.find('.rule_order input').val();	
		
		var data = new Object();
		data.rule_id1 = rule_id1;
		data.rule_id2 = rule_id2;
		data.rule_order1 = order1;
		data.rule_order2 = order2;
		
		var url = base_url + '/shippingtime/swapruleorder';
		$.get(url, data, function(response){			
		});		
		// swap view
		var html1 = tr_tag1.html();
		var html2 = tr_tag2.html();
		
		tr_tag1.hide('fast');
		tr_tag1.html(html2);
		tr_tag1.show('slow');
		
		tr_tag2.hide('fast');		
		tr_tag2.html(html1);				
		tr_tag2.show('slow');	
		
		// swap order					
		tr_tag1.find('.rule_order input').val(order1);
		tr_tag2.find('.rule_order input').val(order2);	
	}
	
	function callbackSaveRule(tr_tag, response)
	{
		disableRuleEdit(tr_tag);	
		if(response.error == -1)
			return;			
		var rule = response.data; 
		var td_rule_comment_tab = tr_tag.find("td.rule_comment .for_view").text(rule.comment);
		var td_rule_comment_tab = tr_tag.find("td.rule_order .for_view").text(rule.order);
		var td_rule_comment_tab = tr_tag.find("td.rule_cond .for_view").html(rule.conditions_parsed);
		var td_rule_comment_tab = tr_tag.find("td.rule_action .for_view").html(rule.action_parsed);
	}
	
	function callbackDeleteRule(tr_tag, response)
	{	
		if(response != 1)
			return;		
		tr_tag.remove();
	}
	
	function parseRule()
	{
		var arr_tr_rule = $("table#rules_list").find("tr.rule_data");
		$.each(arr_tr_rule, function(key, tr_tag){			
			
			var conditions_json =  $(tr_tag).find(".rule_cond textarea").text();
			//var conditions = JSON.stringify(conditions_json);
			//console.log(conditions_json);
			var conditions = jQuery.parseJSON(conditions_json);
			//console.log(conditions);
			$.each(conditions, function(key, condition){				
				//console.log(condition);				
			});
		});
	}
    {/literal}
</script>